﻿package com.lsmo.android.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

import com.lsmo.android.carfans.Global;

/**
 * 
 * @author SYX
 * @类描述：   Caller 相关工具类                                                  
 * @项目名称：carfans     
 * @类名称：Caller                     
 * @修改时间：2012-3-26 
 * @修改备注：   
 */
public class Caller {
	public static String TAG;
	private static RequestCache requestCache = null;
	
	static
	{
	   TAG = "Caller";
	}
	
	/**
	 * 
	 * @param url
	 * @return
	 * @throws WSError 
	 */
	public static String doGet(String url) throws WSError{
		String data = null;
		if(requestCache != null){
			data = requestCache.get(url);
			if(data != null){
				Log.d(Global.TAG, "Caller.doGet [cached] "+url);
				return data;
			}
		}
		
		// initialize HTTP GET request objects
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		HttpResponse httpResponse;
		
		try {
			// execute request
			try {
				httpResponse = httpClient.execute(httpGet);
			} catch (UnknownHostException e) {
				WSError wsError = new WSError();
				wsError.setMessage(e.getLocalizedMessage());
				throw wsError;
			} catch (SocketException e){
				WSError wsError = new WSError();
				wsError.setMessage(e.getLocalizedMessage());
				throw wsError;
			}
			
			// request data
			HttpEntity httpEntity = httpResponse.getEntity();
			
			if(httpEntity != null){
				InputStream inputStream = httpEntity.getContent();
				data = convertStreamToString(inputStream);
				// cache the result
				if(requestCache != null){
					requestCache.put(url, data);
				}
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		Log.d(Global.TAG, "Caller.doGet "+url);
		return data;
	}
	
	//将InputStream类型转换成String类型
	public static String convertStreamToString(InputStream paramInputStream){
		//方法1
//		StringBuffer outStr = new StringBuffer(); 
//	    try {
//			byte[] b = new byte[4096];
//			for (int n; (n = paramInputStream.read(b)) != -1;) { 
//				outStr.append(new String(b, 0, n)); 
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		} 
//	    return outStr.toString();
		//方法2
//	    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
//	    int i=-1; 
//	    try {
//			while((i=paramInputStream.read())!=-1){ 
//				baos.write(i); 
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		} 
//	    return baos.toString();
	    //方法3
	    BufferedReader reader = new BufferedReader(new InputStreamReader(paramInputStream));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				paramInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
	
	public static void setRequestCache(RequestCache paramRequestCache)
	{
	    Caller.requestCache = paramRequestCache;
	 }
	
	public static String createStringFromIds(int[] ids){
		if(ids == null)
			return "";
		
		String query ="";
		
		for(int id : ids){
			query = query + id + "+";
		}
		
		return query;
	}
	
}